<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Course extends Controller
{
    protected $middleware = [
        'CheckAdmin'
    ];

    public function lists(Request $request)
    {
        if ($request->isAjax()) {
            $page = $request->get('page', 1, 'intval');
            $name = $request->get('name', '', 'trim');
            $category_id = $request->get('category_id', 0, 'intval');
            $page = $page > 0 ? $page : 1;
            $limit = $request->get('limit', 10, 'intval');
            $course = Db('course')
                ->alias('c');
            if ($name !== '') {
                $course->where('c.name', 'like', '%' . $name . '%');
            }
            if ($category_id !== 0) {
                $course->where('c.category_id', $category_id);
            }
            $count = $course->count();
            $list = $course
                ->join('category ct', 'c.category_id = ct.category_id', 'LEFT')
                ->field('c.course_id,c.name,c.image,c.price,c.start_time,c.end_time,c.create_time,c.status,ct.name as category_name')
                ->page($page, $limit)
                ->order('c.course_id desc')
                ->select();
            return ['code' => 0, 'data' => $list, 'count' => $count];
        } else {
            return $this->fetch();
        }
    }

    public function addCourse(Request $request)
    {
        if ($request->isPost()) {
            $data = [];
            $data['category_id'] = $request->post('category_id', 0, 'intval');
            $data['name'] = $request->post('name', '', 'trim');
            $data['image'] = $request->post('course_image', '', 'trim');
            $data['description'] = $request->post('description', '', 'trim');
            $data['content'] = $request->post('content', '', 'trim');
            $data['price'] = $request->post('price', 0, 'floatval');
            $data['start_time'] = $request->post('start_time', '', 'trim');
            $data['end_time'] = $request->post('end_time', '', 'trim');
            $data['product_start_time'] = $request->post('product_start_time', 0, 'strtotime');
            $data['product_end_time'] = $request->post('product_end_time', 0, 'strtotime');
            $data['server_start_time'] = $request->post('server_start_time', 0, 'strtotime');
            $data['server_end_time'] = $request->post('server_end_time', 0, 'strtotime');
            $data['teacher_id'] = $request->post('teacher_id', 0, 'intval');
            $data['medal_id'] = $request->post('medal_id', 0, 'intval');
            $data['address'] = $request->post('address', '', 'trim');
            $data['lng'] = $request->post('lng', 0, 'floatval');
            $data['lat'] = $request->post('lat', 0, 'floatval');
            $data['status'] = $request->post('status', 0, 'intval');
            $data['create_time'] = date('Y-m-d H:i:s');
            if ($data['category_id'] == 0) {
                return ['code' => 1, 'msg' => '请选择分类'];
            }
            if ($data['name'] == '') {
                return ['code' => 1, 'msg' => '请填写课程名称'];
            }
            if ($data['price'] == 0) {
                return ['code' => 1, 'msg' => '请填写课程价格'];
            }
            if (false === Db('course')->insert($data)) {
                return ['code' => 1, 'msg' => '操作失败'];
            } else {
                return ['code' => 0, 'msg' => '操作成功'];
            }
        } else {
            $category = Db('category')->order('sort asc')->select();
            $this->assign('category', $category);
            $teacher = Db('teacher')->where('type', 1)->order('sort asc')->select();
            $this->assign('teacher', $teacher);
            $medal = Db('medal')->select();
            $this->assign('medal', $medal);
            return $this->fetch();
        }
    }

    public function editCourse(Request $request)
    {
        if ($request->isPost()) {
            $data = [];
            $course_id = $request->post('course_id', 0, 'intval');
            $data['category_id'] = $request->post('category_id', 0, 'intval');
            $data['name'] = $request->post('name', '', 'trim');
            $data['image'] = $request->post('course_image', '', 'trim');
            $data['description'] = $request->post('description', '', 'trim');
            $data['content'] = $request->post('content', '', 'trim');
            $data['price'] = $request->post('price', 0, 'floatval');
            $data['start_time'] = $request->post('start_time', '', 'trim');
            $data['end_time'] = $request->post('end_time', '', 'trim');
            $data['product_start_time'] = $request->post('product_start_time', 0, 'strtotime');
            $data['product_end_time'] = $request->post('product_end_time', 0, 'strtotime');
            $data['server_start_time'] = $request->post('server_start_time', 0, 'strtotime');
            $data['server_end_time'] = $request->post('server_end_time', 0, 'strtotime');
            $data['teacher_id'] = $request->post('teacher_id', 0, 'intval');
            $data['medal_id'] = $request->post('medal_id', 0, 'intval');
            $data['address'] = $request->post('address', '', 'trim');
            $data['lng'] = $request->post('lng', 0, 'floatval');
            $data['lat'] = $request->post('lat', 0, 'floatval');
            $data['status'] = $request->post('status', 0, 'intval');
            if ($data['category_id'] == 0) {
                return ['code' => 1, 'msg' => '请选择分类'];
            }
            if ($data['name'] == '') {
                return ['code' => 1, 'msg' => '请填写课程名称'];
            }
            if ($data['price'] == 0) {
                return ['code' => 1, 'msg' => '请填写课程价格'];
            }
            if (false === Db('course')->where('course_id', $course_id)->update($data)) {
                return ['code' => 1, 'msg' => '操作失败'];
            } else {
                return ['code' => 0, 'msg' => '操作成功'];
            }
        } else {
            $course_id = $request->get('id', 0, 'intval');
            $info = Db('course')->where('course_id', $course_id)->find();
            $this->assign('info', $info);
            $category = Db('category')->order('sort asc')->select();
            $this->assign('category', $category);
            $teacher = Db('teacher')->where('type', 1)->order('sort asc')->select();
            $this->assign('teacher', $teacher);
            $medal = Db('medal')->select();
            $this->assign('medal', $medal);
            return $this->fetch();
        }
    }

    public function deleteCourse(Request $request)
    {
        $course_id = $request->get('id', 0, 'intval');
        if (false === Db('course')->where('course_id', $course_id)->delete()) {
            return ['code' => 1, 'msg' => '操作失败'];
        } else {
            return ['code' => 0, 'msg' => '操作成功'];
        }
    }
}